package com.yuncheng.oaApi.controller;


import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaBusinessConstant;
import com.yuncheng.oaApi.constant.OaLgqsOaFlowConstants;
import com.yuncheng.spcyApi.utils.MyExcelUtils;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.entity.OaGwyc;
import com.yuncheng.oaApi.service.IOaGwycService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description: 公务用车
 * @Author: lhb
 * @Date: 2023-06-30
 * @Version: V1.0
 */
@Api(tags = "公务用车接口")
@RestController
@RequestMapping("/api/spzx-oa/oaGwyc")
public class OaGwycController extends BaseController<OaGwyc, IOaGwycService> {

    private static final Logger log = LoggerFactory.getLogger(OaGwycController.class);

    @Resource
    private IOaGwycService oaGwycService;

    @Resource
    private MyExcelUtils oaMyExcelUtils;


    /**
     * 分页列表查询
     *
     * @param oaGwyc
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "公务用车-分页列表查询")
    @ApiOperation(value = "公务用车-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(OaGwyc oaGwyc, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<OaGwyc> queryWrapper = QueryBuilder.initQueryWrapper(oaGwyc, req.getParameterMap());
        Page<OaGwyc> page = new Page<>(pageNo, pageSize);
        IPage<OaGwyc> pageList = oaGwycService.page(page, queryWrapper);
        for(OaGwyc vo:pageList.getRecords()){
            formatValue(vo);
        }
        return HttpResult.ok(pageList);
    }

//
//
//
//    /**
//     * 编辑
//     *
//     * @param oaGwyc
//     * @return
//     */
//    @AutoLog(value = "公务用车-编辑")
//    @ApiOperation(value = "公务用车-编辑")
//    //@RequiresPermissions("oa-gwyc:OaGwyc:edit")
//    @PutMapping(value = "/edit")
//    public HttpResult<?> edit(@RequestBody OaGwyc oaGwyc) {
//        oaGwycService.updateById(oaGwyc);
//        return HttpResult.ok(I18nUtil.message("update.success"));
//    }
//
//    /**
//     * 通过id删除
//     *
//     * @param id
//     * @return
//     */
//    @AutoLog(value = "公务用车-通过id删除")
//    @ApiOperation(value = "公务用车-通过id删除")
//    //@RequiresPermissions("oa-gwyc:OaGwyc:delete")
//    @DeleteMapping(value = "/delete")
//    public HttpResult<?> delete(@RequestParam("id") String id) {
//        oaGwycService.removeById(id);
//        return HttpResult.ok(I18nUtil.message("delete.success"));
//    }
//
//    /**
//     * 通过id批量删除
//     *
//     * @param ids
//     * @return
//     */
//    @AutoLog(value = "公务用车-批量删除")
//    @ApiOperation(value = "公务用车-批量删除")
//    //@RequiresPermissions("oa-gwyc:OaGwyc:delete")
//    @DeleteMapping(value = "/deleteBatch")
//    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
//        oaGwycService.removeByIds(Arrays.asList(ids.split(",")));
//        return HttpResult.ok(I18nUtil.message("delete.success"));
//    }
//
//    /**
//     * 通过id查询
//     *
//     * @param id
//     * @return
//     */
//    @AutoLog(value = "公务用车-通过id查询")
//    @ApiOperation(value = "公务用车-通过id查询")
//    @GetMapping(value = "/queryById")
//    public HttpResult<?> queryById(@RequestParam("id") String id) {
//        OaGwyc oaGwyc = oaGwycService.getById(id);
//        return HttpResult.ok(oaGwyc);
//    }

    /**
     * 数据转换
     * @param oaGwyc
     */
    private void formatValue(OaGwyc oaGwyc){
    }


    @AutoLog(value = "导出公务用车记录数据-表格excel")
    @ApiOperation(value = "导出公务用车记录数据-表格excel")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids",value = "主键id字符逗号分割",required = true)
    })
    @GetMapping("/exportGwycData")
    public void exportGwycData(@RequestParam(value = "ids",defaultValue = "")String ids,
                               OaGwyc ndkpBasic,
                               HttpServletResponse response){
        try {
            String name = "exportGwycData";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name+".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

            List<String> IdsList = Arrays.asList(ids.split(","));
            List<Object> list = new ArrayList<>(); // 导出的数据
            // 多选导出
            if (StringUtils.isNotEmpty(ids)) {
                List<OaGwyc> ndkpBasicList = oaGwycService.listByIds(IdsList);
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaGwyc e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }else {
                LambdaQueryWrapper<OaGwyc> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(StringUtils.isNotBlank(ndkpBasic.getFuserid()),OaGwyc::getFuserid,ndkpBasic.getFuserid());
                //用车部门
                queryWrapper.eq(StringUtils.isNotBlank(ndkpBasic.getFycbm()),OaGwyc::getFycbm, ndkpBasic.getFycbm());
                //乘车人
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFccr()),OaGwyc::getFccr,ndkpBasic.getFccr());
                //起讫地点
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFqqdd()),OaGwyc::getFqqdd,ndkpBasic.getFqqdd());


                if (StringUtils.isBlank(ndkpBasic.getFlczt())) {

                }else if (ndkpBasic.getFlczt().equals(OaLgqsOaFlowConstants.HJMC_LCJS)){
                    queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFlczt()), OaGwyc::getFlczt, OaLgqsOaFlowConstants.HJMC_LCJS);
                }else {
                    queryWrapper.isNull(OaGwyc::getFlczt);
                }
                if (ndkpBasic.getFccr() != null){
                    queryWrapper.le(OaGwyc::getFccr,ndkpBasic.getFccr());
                }
                if (ndkpBasic.getFycbm() != null){
                    queryWrapper.ge(OaGwyc::getFycbm,ndkpBasic.getFycbm());
                }
                List<OaGwyc> ndkpBasicList = oaGwycService.list(queryWrapper);
                // 条件导出
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaGwyc e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }
            oaMyExcelUtils.outputListToExclFile(OaBusinessConstant.IMPORT_OA_GWYC_XML,list,out); // 导出模板数据成excel
        } catch (Exception e) {
            log.error("导出公务用车信息数据出错-{}",e.getMessage());
        }
    }
}
